package com.ljf.common.utils;

import com.ljf.common.enums.ResultCodeEnum;
import com.ljf.common.result.QueryResultModel;
import com.ljf.common.result.ResultModel;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * web返回数据构建工具类
 *
 * @author LJF
 * @since 2024-01-26
 */
public class ResultUtil {

    private ResultUtil() {

    }

    /**
     * 查询成功
     *
     * @param list
     * @return
     */
    public static<T extends Serializable> ResultModel success(List<T> list) {
        QueryResultModel<T> model = new QueryResultModel<>();
        model.setCode(ResultCodeEnum.OK.getCode());
        model.setInfo("查询成功");
        model.setMsg("查询成功");
        model.setTimestamp(System.currentTimeMillis());
        model.setRows(list == null ? Collections.emptyList() : list);
        model.setTotal(list == null ? 0L : list.size());
        return model;
    }

    /**
     * 请求失败
     *
     * @param msg
     * @return
     */
    public static<T extends Serializable> ResultModel fail(String msg) {
        QueryResultModel<T> model = new QueryResultModel<>();
        model.setCode(ResultCodeEnum.ERROR.getCode());
        model.setInfo(msg);
        model.setMsg(msg);
        model.setTimestamp(System.currentTimeMillis());
        return model;
    }

}
